/**
 * @filename:TmReportCommissionServiceImpl ReportCommission
 * @project wallet-sign  V1.0
 * Copyright(c) 2018 BianPeng Co. Ltd. 
 * All right reserved. 
 */
package com.yixun.qfbao.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.ReportCommissionMapper;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.ReportCommission;
import com.yixun.qfbao.model.dto.CommissionSummaryDto;
import com.yixun.qfbao.model.dto.CustomerPremisesCommissionDto;
import com.yixun.qfbao.model.dto.IndexCommissionStatisticsDto;
import com.yixun.qfbao.model.dto.ReportCommissionDto;
import com.yixun.qfbao.model.enums.CommissionStatusEnum;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.MechanismAccountTypeEnum;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.service.ReportCommissionService;
import com.yixun.qfbao.xf.model.CustomerPremises;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesDto;
import com.yixun.qfbao.xf.service.CustomerPremisesService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**   
 *  
 * @Description:  报备佣金信息表——SERVICEIMPL
 * @Author:       BianPeng   
 * @CreateDate:   ReportCommission
 * @Version:      V1.0
 *    
 */
@Service
public class ReportCommissionServiceImpl implements ReportCommissionService {

	@Autowired
	private ReportCommissionMapper reportCommissionMapper;

	@Autowired
	private PartnerInfoService partnerInfoService;

	@Autowired
	private CustomerPremisesService customerPremisesService;

	@Override
	public ReportCommission selectById(Long id) {
		return reportCommissionMapper.selectById(id);
	}

	@Override
	public int deleteById(Long id) {
		return reportCommissionMapper.deleteById(id);
	}

	@Override
	public int insert(ReportCommission reportCommission) {
		return reportCommissionMapper.insert(reportCommission);
	}

	@Override
	public int update(ReportCommission reportCommission) {
		return reportCommissionMapper.update(reportCommission);
	}

	@Override
	public List<ReportCommission> queryReportCommissionList(ReportCommission reportCommission) {
		return reportCommissionMapper.queryTmReportCommissionList(reportCommission);
	}

	@Override
	public ReportCommission addForReport(ReportCommission reportCommission, Long operationId) {
		PartnerInfo pi = partnerInfoService.selectById(operationId);
		if(reportCommission.getDevCompanyId() == null){
			reportCommission.setDevCompanyId(pi.getHigherid());
		}
		reportCommission.setCreateUserId(operationId);
		reportCommission.setRuleStatus("0");
		reportCommissionMapper.insert(reportCommission);
		return reportCommission;
	}

	@Override
	public ReportCommission selectOne(ReportCommissionDto dto) {
		return reportCommissionMapper.selectOne(dto);
	}

	@Override
	public void saveOrUpdateForReport(ReportCommission reportCommission, CustomerPremisesDto cpDto, Long operationId) {
		PartnerInfo pi = partnerInfoService.selectById(operationId);
		if(reportCommission.getDevCompanyId() == null){
			reportCommission.setDevCompanyId(pi.getHigherid());
		}
		if(reportCommission.getId() == null){
			// 初始化新增数据
			ReportCommission reportCommissionNew = buildNewReportCommission(cpDto);
			reportCommissionNew.setCommissionId(reportCommission.getCommissionId());
			reportCommissionNew.setAgentCommission(reportCommission.getAgentCommission() == null
					? BigDecimal.ZERO : reportCommission.getAgentCommission());
			reportCommissionNew.setAgentPendingCommission(reportCommission.getAgentPendingCommission() == null
					? BigDecimal.ZERO : reportCommission.getAgentPendingCommission());
			reportCommissionNew.setCreateUserId(operationId);
			reportCommissionNew.setDevCompanyId(reportCommission.getDevCompanyId());
			reportCommissionNew.setRuleStatus(reportCommission.getRuleStatus());
			calculationOrgCommission(reportCommissionNew);
			reportCommissionMapper.insert(reportCommissionNew);
		}else{
			reportCommission.setUpdateTime(new Date());
			reportCommission.setUpdateUserId(operationId);
			calculationOrgCommission(reportCommission);
			reportCommissionMapper.update(reportCommission);
		}
		if(reportCommission.getAgentCommission() != null && BigDecimal.ZERO.compareTo(reportCommission.getAgentCommission()) < 0) {
			CustomerPremises cp = customerPremisesService.selectById(reportCommission.getReportId());
			if(cp == null){
				return;
			}
			CustomerPremisesDto entity = new CustomerPremisesDto();
			entity.setId(cp.getId());
			entity.setCommissionStatus(com.yixun.qfbao.xf.model.enums.CommissionStatusEnum.no_commission.getNo());
			entity.setCommissionAmount(reportCommission.getAgentCommission().toString());
			customerPremisesService.update(entity);
		}
	}

	// 初始化新增数据
	private ReportCommission buildNewReportCommission(CustomerPremisesDto cpDto){
		ReportCommission rc = new ReportCommission();
		rc.setDevCommission(rc.getDevCommission() == null ? BigDecimal.ZERO : rc.getDevCommission());
		rc.setDevPendingCommission(rc.getDevPendingCommission() == null ? BigDecimal.ZERO : rc.getDevPendingCommission());
		rc.setDevReceivedCommission(rc.getDevReceivedCommission() == null ? BigDecimal.ZERO : rc.getDevReceivedCommission());
		rc.setDevCommissionStatus(CommissionStatusEnum.COMMISSIONSTATUS_VALUE_0.getCode());
		rc.setAgentCommission(rc.getAgentCommission() == null ? BigDecimal.ZERO : rc.getAgentCommission());
		rc.setAgentPendingCommission(rc.getAgentPendingCommission() == null ? BigDecimal.ZERO : rc.getAgentPendingCommission());
		rc.setAgentReceivedCommission(rc.getAgentReceivedCommission() == null ? BigDecimal.ZERO : rc.getAgentReceivedCommission());
		rc.setAgentCommissionStatus(CommissionStatusEnum.COMMISSIONSTATUS_VALUE_0.getCode());
		rc.setOrgCommission(rc.getOrgCommission() == null ? BigDecimal.ZERO : rc.getOrgCommission());
		rc.setOrgPendingCommission(rc.getOrgPendingCommission() == null ? BigDecimal.ZERO : rc.getOrgPendingCommission());
		rc.setOrgReceivedCommission(rc.getOrgReceivedCommission() == null ? BigDecimal.ZERO : rc.getOrgReceivedCommission());
		rc.setAgentCompanyId(cpDto.getStaffCompanyId());
		rc.setPremisesId(cpDto.getPremisesId());
		rc.setReportId(cpDto.getId());
		rc.setDelFlag(DelFlagEnum.UNDEL.getNo());
		rc.setCreateTime(new Date());
		return rc;
	}

	// 计算本部佣金
	private void calculationOrgCommission(ReportCommission reportCommission){
		BigDecimal commission = reportCommission.getDevCommission().subtract(reportCommission.getAgentCommission());
		if(commission.compareTo(BigDecimal.ZERO) >= 0){
			reportCommission.setOrgCommission(commission);
		}
		BigDecimal pendingCommission = reportCommission.getDevPendingCommission().subtract(reportCommission.getAgentPendingCommission());
		if(pendingCommission.compareTo(BigDecimal.ZERO) >= 0){
			reportCommission.setOrgPendingCommission(pendingCommission);
		}
	}

	@Override
	public Page<CommissionSummaryDto> selectCommissionSummaryDtoList(CommissionSummaryDto entity, int begin, int end, String accountType, String partnerId) {
		Integer total;
		List<CommissionSummaryDto> rows;
		if(MechanismAccountTypeEnum.mechanism.getNo().equals(accountType)){
			total = reportCommissionMapper.myCount(entity);
			rows = reportCommissionMapper.myList(entity,begin,end);
		}else{
			total = reportCommissionMapper.selectCommissionForCaseMemberCount(entity,partnerId);
			rows = reportCommissionMapper.selectCommissionForCaseMember(entity,begin,end,partnerId);
		}
		Page<CommissionSummaryDto> msr = new Page<CommissionSummaryDto>(total, rows);
		return msr;
	}

	@Override
	public Page<CustomerPremisesCommissionDto> selectCustomerPremisesCommissionDtoList(CustomerPremisesCommissionDto entity, int begin, int end) {
		Integer total = reportCommissionMapper.selectCustomerPremisesCommissionLCount(entity);
		List<CustomerPremisesCommissionDto> rows = reportCommissionMapper.selectCustomerPremisesCommissionList(entity,begin,end);
		Page<CustomerPremisesCommissionDto> msr = new Page<CustomerPremisesCommissionDto>(total, rows);
		return msr;
	}

	@Override
	public CommissionSummaryDto selectCommissionByPremisesAndCompany(CommissionSummaryDto entity) {
		return reportCommissionMapper.selectCommissionByPremisesAndCompany(entity);
	}

	@Override
	public void initDevCompanyId() {
		List<ReportCommission> list = reportCommissionMapper.selectDevCompanyIdList();
		if (CollectionUtils.isNotEmpty(list)) {
			ReportCommission rc = new ReportCommission();
			list.stream().forEach(item -> {
				rc.setId(item.getId());
				rc.setDevCompanyId(item.getDevCompanyId());
				if (item.getDevCompanyId() != null) {
					reportCommissionMapper.update(item);
				}
			});
		}
	}

	@Override
	public IndexCommissionStatisticsDto getIndexCommissionStatistics(IndexCommissionStatisticsDto commissionDto) {
		IndexCommissionStatisticsDto a = reportCommissionMapper.getIndexCommissionStatistics(commissionDto);
		return a;
	}
}